package llc.ufwa.unlimitedlist.activities.list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import llc.ufwa.activities.injecting.AbstractInjectingActivity;
import llc.ufwa.activities.injecting.InjectableController;
import llc.ufwa.activities.injecting.InjectingDisplay;
import llc.ufwa.activities.injecting.RunOnUIDisplay;
import llc.ufwa.concurrency.Callback;
import llc.ufwa.concurrency.Debouncer;
import llc.ufwa.concurrency.DebouncingExecutor;
import llc.ufwa.concurrency.RunOnActivityUIExecutor;
import llc.ufwa.unlimitedlist.R;
import llc.ufwa.util.StopWatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.widget.ListView;

public class UnlimitedListActivity extends AbstractInjectingActivity {
	
	private static final Logger logger = LoggerFactory.getLogger(UnlimitedListActivity.class);
	
	private static final ListController listController = new ListController(ListDisplay.class, Executors.newFixedThreadPool(4));
  
	private final ListDisplay display =
	    new RunOnUIDisplay<ListDisplay>(
			new DebouncingExecutor(new RunOnActivityUIExecutor(this), Executors.newSingleThreadExecutor(), 1000),
			
	        new ListDisplay() {
				
			},
	        
	        ListDisplay.class
	    ).getWrapped();
	
	@Override
	protected Set<InjectableController<?>> getControllers() {
		
		final Set<InjectableController<?>> controllers = new HashSet<InjectableController<?>>();
		controllers.add(listController);
		
		return controllers;
	}

	@Override
	protected Set<InjectingDisplay> getDisplays() {
		
		final Set<InjectingDisplay> displays = new HashSet<InjectingDisplay>();
		displays.add(display);
		
		return displays;
		
	}

	@Override
	protected void onAppInitialized() {
		
		this.setContentView(R.layout.titled_list);
		
		final ListView list = (ListView)this.findViewById(android.R.id.list);
		
		list.setAdapter(
		    new ListAdapter(
		        this,
		        new Executor(){

                    @Override
                    public void execute(Runnable command) {
                    	UnlimitedListActivity.this.runOnUiThread(command);                                            
                    }
                }, 
                getList(10000)
            )
		);
		
	}

	@Override
	protected void onAppInitializing() {
		
	}
	
	private List<ListKey> getList(final int SIZE) {
		
		final List<ListKey> keys = new ArrayList<ListKey>(SIZE);
		
		for(long i = 0; i < SIZE; i++) {
			keys.add(new ListKey(i));
		}
		
		return keys;
		
	}
	
}